# -*- coding:utf-8 -*-
import json
from datetime import datetime
def parse_log(path):
    po = Parse2Obj(path)
    data = po.parse_file()
    c_util = CountData(data)
    count_data = c_util.count()
    c_util.print_data(count_data);


class CountData():
    ''' 
        统计数据
    '''
    data = None
    count_data = {}
    def __init__(self,data):
        self.data = data

    def count(self):
        if self.data is None : return
        ip_list = []  #IP容器,排除重复的IP
        uv = 0
        result = 0 #有结果
        un_result = 0 #无结果
        channel_map = {} #渠道
        suggest_map = {} #下拉提示点击位置
        result_key = [] #有结果关键字
        un_result_key = [] #无结果关键字
        start_time = datetime.now().microsecond
        for o in self.data:
            ip_list.append(str(o['ip']))
            if str(o['from']) not in channel_map :
                channel_map[str(o['from'])] = []
            channel_map[str(o['from'])].append(str(o['ip']))
            if str(o['suggest']) not in suggest_map :
                suggest_map[str(o['suggest'])] = []
            suggest_map[str(o['suggest'])].append(str(o['ip']))
            if str(o['result_count']).isdigit()  and int(o['result_count']) <= 0 :
                un_result = un_result + 1 
                un_result_key.append(o['rewriteword'])
            else:
                result = result + 1
                result_key.append(o['rewriteword'])
            
        self.count_pv() 
        self.count_uv(ip_list) 
        self.count_channel(channel_map) 
        self.count_suggest(suggest_map) 
        self.count_result(result, un_result) 
        self.count_key(result_key, un_result_key) 
        end_time = datetime.now().microsecond
        print u'分析数据耗时 ： %f 毫秒' % (float(abs(end_time-start_time))/1000)
        #print_data(count_data)
        return self.count_data

    def count_channel(self, channel_map):
        channel_list = []
        for key in channel_map.keys():
            channel_pv = len(channel_map[key])
            channel_uv = len(set(channel_map[key]))
            channel_key = key
            channel = {key:channel_key,'pv':channel_pv,'uv':channel_uv}
            channel_list.append(channel)
        self.count_data['channel'] = channel_list 

    def count_suggest(self, suggest_map):
        suggest_list = []
        for suggest in suggest_map.keys():
            suggest_num = len(suggest_map[suggest])
            suggest_list.append({'suggest':suggest,'num':suggest_num})
        self.count_data['suggest'] = suggest_list

    def count_key(self, result_key, un_result_key):
        result_keys = set(result_key)
        key_count_map = {}
        for w_key in result_keys:
            count = 0
            for word in result_key:
                if word == w_key :
                    count = count + 1
            key_count_map[w_key] = count
        key_count_list = sorted(key_count_map.items(), key=lambda x:x[1], reverse=True)
        top_count = {}
        t_count = 0;
        for t in key_count_list:
            top_count[t[0]] = t[1]
            t_count = t_count + 1
            if t_count > 10:
                break
        self.count_data['key'] = top_count 
        
    def count_pv(self):
        pv = len(self.data)
        self.count_data['pv'] = pv

    def count_uv(self, ip_list):
        ip_set = set(ip_list)
        uv = len(ip_set)
        self.count_data['uv'] = uv

    def count_result(self, result, un_result):
        s_result = str(result)
        s_un_result = str(un_result)
        per_result = str(float(result)/float(result+un_result))
        per_un_result = str(float(un_result)/float(result+un_result))
        self.count_data['result'] = s_result 
        self.count_data['un_result'] = s_un_result 
        self.count_data['per_result'] = per_result
        self.count_data['per_un_result'] = per_un_result

    def print_data(self, count_data):
        '''
            打印统计数据
        '''
        print 'pv : ' , count_data['pv'] 
        print '\n'
        print 'uv : ' , count_data['uv']
        print '\n'
        print 'result : ' , count_data['result']
        print '\n'
        print 'un_result : ' , count_data['un_result']
        print '\n'
        print 'per of result : ' , count_data['per_result']
        print '\n'
        print 'per of un_result : ' , count_data['per_un_result']
        print '\n'
        print 'key word top 10 : ' 
        key_words = count_data['key']
        key_words_list = sorted(key_words.items(),key=lambda x:x[1],reverse=True)
        for key_word in key_words_list:
            print key_word[0] + ' : ' + str(key_word[1]) + '; ' ,
        print '\n'
        print 'suggest : ' ,repr(count_data['suggest'])
        print '\n'
        print 'channel : ' , repr(count_data['channel'])
#-----------------分割线-----------------------
class Parse2Obj():
    '''  
        转换成数据对象
    '''
    def __init__(self,path):
        self.path = path 

    #解析一行数据
    #return a dict
    def parse_file(self):
        data = []
        f = None
        lines = None
        start_time = datetime.now().microsecond
        try:
            print 'parse start ... '
            f = open(self.path,'r')
            lines = f.readlines()
        except IOError as e:
            print e
        finally:
            if f is not None:
                f.close()
        if lines is None: return data
        #遍历每行文档读取数据
        print 'The lines of File : ' , str(len(lines))
        for line in lines:
            o = json.loads(line)
            data.append(o)
            if len(data) >= 100000 :
                break
        print 'parse file ok ... '
        print 'total lines : ' + str(len(data))
        end_time = datetime.now().microsecond
        print u'解析文件耗时 : %f 毫秒' % ((float(abs(end_time - start_time)))/1000)
        print u'解析文件耗时 : %f 秒' % (float(abs(end_time - start_time))/float(1000*1000))
        print '\n'
        return data;


